package handler

import (
	"fmt"
	config "goweb/config"
	"goweb/service"
	"html/template"
	"net/http"

	"goweb/util"
)

//HandleConfig URI配置
func HandleConfig() {
	// 静态文件
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(config.GetDir()+"static"))))
	//首页
	http.HandleFunc("/index", index)
	http.HandleFunc("/", index)

	//登录
	http.HandleFunc("/login", Login)
	//用户
	http.HandleFunc("/user/userInfo", UserInfo)

	path := util.GetPath()
	fmt.Println(path)
}

func index(w http.ResponseWriter, r *http.Request) {
	var cookie, _ = r.Cookie("userId")
	if cookie != nil {
		user, _ := service.GetUser(cookie.Value)
		template.Must(template.ParseFiles(config.GetDir()+"html/index.html")).ExecuteTemplate(w, "user", user)
	} else {
		http.Redirect(w, r, "/login", http.StatusFound)
	}
}
